package com.kele.controller;

import com.github.pagehelper.PageInfo;
import com.kele.pojo.User;
import com.kele.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/admin/hello")
    @ResponseBody
    public String admin() {
        return "hello admin";
    }

    @RequestMapping("/user/hello")
    @ResponseBody
    public String user() {
        return "hello user";
    }

    @RequestMapping("/admin/userList")
    public String userList(Model model,
                           @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                           @RequestParam(defaultValue = "5", value = "pageSize") Integer pageSize,
                           @RequestParam(name = "username", required = false) String username){
        Map<Object, Object> queryDate = new HashMap<>();
        if (username != null && !(username.equals(""))){
            queryDate.put("username", username);
        }
        PageInfo<User> allUser = userService.getAllUser(pageNum, pageSize, queryDate);
        model.addAttribute("allUser", allUser);
        model.addAttribute("username", username);
        return "admin/userList";
    }

}
